<template>
  <div class="brand-selection-container">
    <!-- 顶部提示信息 -->
    <div class="tip-box" v-if="action !== 'setDiscount'">
      <div class="tip-icon">?</div>
      <div class="tip-text">
        设置的返点比例将会同步到业务员结算处，通用折扣以及普通折扣则为影响店铺的报价单，请谨慎设置。
      </div>
    </div>

    <!-- 品牌选择区域 -->
    <div class="form-section">
      <div class="form-item" v-if="action === 'returnPointRatio'">
        <div class="form-label required">返点比例</div>
        <div class="form-value">
          <input
            v-model="form.return_point_ratio"
            type="digit"
            class="form-input"
            placeholder="请输入返点比例"
          />
        </div>
      </div>

      <div class="set-discount-box" v-if="action === 'setDiscount'">
        <div class="discount-label-title">
          <span>设置规格组：</span>
          <span>
            <button class="action-btn add-btn" @click="addSetDiscount">
              添加
            </button>
          </span>
        </div>
        <div v-for="(item, index) in SkuDiscount" :key="index" style="margin-bottom: 10rpx;">
          <div class="discount-box">
            <div class="item">
              <input
                type="text"
                placeholder="请输入规格组名称"
                v-model="item.discount"
              />
              <button class="action-btn del-btn" @click="delSetDiscount(index)" v-show="index !== 0">
                删除
              </button>
              <button
                class="action-btn btn-view"
                @click="setOtherDiscount(index)"
              >
                选择剩余
              </button>
            </div>
            <MultiSelect
              label="选择规格"
              :required="true"
              placeholder="请选择配置的规格"
              :options="skuList"
              v-model="SkuDiscount[index].goods_spec_value_ids"
            />
          </div>
        </div>
      </div>

      <div v-if="action === 'setGeneralDiscount'">
        <view
          class="product-section"
          v-for="item in discountList"
          :key="item.id"
        >
          <view class="product-title">{{ item.goods_name }}</view>
          <view class="product-spec">规格：{{ item.discount }}</view>

          <view class="discount-row-container">
            <view
              class="discount-row"
              style="width: 45%"
              v-for="(sku, index) in item.goods_sku_discount"
              :key="index"
            >
              <view class="discount-label">折扣{{ index + 1 }}:</view>
              <input
                v-model="sku.discount"
                type="digit"
                class="discount-input"
                placeholder="请输入"
              />
            </view>
          </view>
        </view>
      </div>
    </div>

    <!-- 底部保存按钮 -->
    <div class="footer-btn">
      <button class="save-btn" @click="saveSelection">保存</button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from "vue";
import MultiSelect from "@/component/selectMore/selectmore.vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import {
  setReturnPointRatio,
  getReturnPointRatio,
  getFromAdminDiscount,
  setDiscount,
  getFromAdminGeneralDiscount,
  setGeneralDiscount,
} from "@/api/goods";
import { showToast } from "@/utils/helper";

const form = reactive({
  id: undefined,
  brand_id: undefined,
  return_point_ratio: undefined,
});

const skuList = ref([]);
const SkuDiscount = ref([
  {
    discount: "",
    goods_spec_value_ids: [],
    goods_discount_id: 0,
  },
]);

// 保存选择
const saveSelection = async () => {
  switch (action.value) {
    case "returnPointRatio":
      await setReturnPointRatio({
        id: form.id,
        brand_id: form.brand_id,
        return_point_ratio: form.return_point_ratio,
      });
      break;
    case "setDiscount":
      if (
        SkuDiscount.value.some(
          (i) => i.discount === "" || i.goods_spec_value_ids.length === 0
        )
      )
        return message("存在为空的折扣信息", "error");
      await setDiscount({
        id: form.id,
        brand_id: form.brand_id,
        set_discount: JSON.stringify(
          SkuDiscount.value.map((i) => ({ ...i, goods_id: form.id }))
        ),
      });
      break;
    case "setGeneralDiscount":
      const arr = discountList.value
        .map((i) => i.goods_sku_discount)
        .flat()
        .filter((c) => c.discount != 0);
      await setGeneralDiscount({
        id: form.id,
        brand_id: form.brand_id,
        goods_sku_discount: arr,
      });
      break;
  }
  uni.showToast({
    title: "保存成功",
    icon: "success",
  });
  uni.navigateBack();
};

// 获取sku商品数据
const getSkuGoods = async () => {
  const res = await getFromAdminDiscount({
    id: form.id,
    brand_id: form.brand_id,
  });
  skuList.value = res.goods_sku_data.map((i) => ({
    id: i.goods_spec_value_ids,
    name: i.goods_spec_value_names,
  }));
  res.set_discount.length !== 0 && (SkuDiscount.value = res.set_discount);
};

const addSetDiscount = () => {
  SkuDiscount.value.push({
    discount: "",
    goods_spec_value_ids: [],
    goods_discount_id: 0,
  });
};

const delSetDiscount = (index) => {
  SkuDiscount.value.splice(index, 1);
};

const setOtherDiscount = (index) => {
  const allSelectSkuDiscount_values = [
    ...new Set(SkuDiscount.value.map((ids) => ids.goods_spec_value_ids).flat()),
  ];
  const other = skuList.value
    .filter((ic) => !allSelectSkuDiscount_values.includes(ic.id))
    .map((ic) => ic.id);
  if (other.length == 0) return showToast("已经全部选中了");
  SkuDiscount.value[index].goods_spec_value_ids = [
    ...SkuDiscount.value[index].goods_spec_value_ids,
    ...other,
  ];
};

const discountList = ref([]);
const getSetDiscountList = async () => {
  const res = await getFromAdminGeneralDiscount({
    id: form.id,
    brand_id: form.brand_id,
  });
  discountList.value = res;
};

const action = ref("");
onLoad(async (options) => {
  const { id, brand_id, return_point_ratio } = JSON.parse(options.item);
  action.value = options.type;
  Object.assign(form, { id, brand_id, return_point_ratio });
});

onShow(async () => {
  switch (action.value) {
    case "returnPointRatio":
      const res = await getReturnPointRatio({
        id: form.id,
        brand_id: form.brand_id,
      });
      form.return_point_ratio = res.return_point_ratio;
      break;
    case "setDiscount":
      await getSkuGoods();
      break;
    case "setGeneralDiscount":
      getSetDiscountList();
      break;
  }
});
</script>

<style scoped lang="scss">
.form-item {
  display: flex;
  padding: 30rpx 0;
  border-bottom: 1px solid #eee;
  position: relative;
}

.form-label {
  width: 180rpx;
  font-size: 28rpx;
  color: #333;
}

.required::before {
  content: "*";
  color: #ff0000;
  margin-right: 4rpx;
}

.form-value {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.form-input {
  flex: 1;
  font-size: 28rpx;
  color: #333;
  height: 40rpx;
  border: none;
}
.brand-selection-container {
  min-height: 100vh;
  background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
}

/* 顶部提示信息样式 */
.tip-box {
  display: flex;
  align-items: flex-start;
  background-color: #ffffff;
  padding: 20rpx 30rpx;
  margin-bottom: 20rpx;
}

.tip-icon {
  width: 40rpx;
  height: 40rpx;
  background-color: #f2f2f2;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
  font-size: 28rpx;
  margin-right: 20rpx;
  flex-shrink: 0;
}

.tip-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

/* 表单区域样式 */
.form-section {
  background-color: #ffffff;
  padding: 0 30rpx;
  flex: 1;
}

/* 底部按钮样式 */
.footer-btn {
  width: 100%;
  padding: 0;
  position: fixed;
  bottom: 0;
  left: 0;
}

.save-btn {
  width: 100%;
  height: 100rpx;
  line-height: 100rpx;
  background-color: #1989fa;
  color: #ffffff;
  font-size: 32rpx;
  text-align: center;
  border: none;
  border-radius: 0;
}

.action-btn {
  min-width: 140rpx;
  height: 60rpx;
  text-align: center;
  font-size: 24rpx;
  border-radius: 10rpx;
  padding: 0 30rpx;
  margin: 0;
  white-space: nowrap;
}

.btn-view {
  background-color: #f5f5f5;
  color: #666;
}
.add-btn {
  background-color: #24adf3;
  color: #fff;
}
.del-btn{
  background-color: red;
  color: #fff;
}
.set-discount-box {
  margin: 20rpx;
  .discount-label-title {
    margin-bottom: 10rpx;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
  }
  .discount-box {
    display: flex;
    gap: 10rpx;
    flex-direction: column;
    .item {
      display: flex;
      gap: 10rpx;
      align-items: center;
      input {
        padding: 10rpx 20rpx;
        border: 1px solid #ccc;
        width: 46%;
      }
    }
  }
}
.discount-setting {
  padding: 40rpx;

  .discount-list {
    display: flex;
    flex-direction: column;
    gap: 40rpx;
  }
  .dis-label {
    font-size: 28rpx;
    color: #606266;
    margin: 20rpx 40rpx;
  }

  .discount-item {
    background: #fff;
    border-radius: 16rpx;
    box-shadow: 0 4rpx 24rpx 0 rgba(0, 0, 0, 0.05);
    overflow: hidden;

    .discount-header {
      padding: 32rpx 40rpx;
      background: #f5f7fa;
      border-bottom: 2rpx solid #ebeef5;

      .goods-name {
        margin: 0 0 16rpx 0;
        font-size: 32rpx;
        font-weight: 600;
        color: #303133;
      }

      .spec-info {
        font-size: 28rpx;
        color: #606266;
      }
    }

    .discount-content {
      padding: 32rpx 40rpx;
      display: flex;
      flex-wrap: wrap;
      .discount-row {
        display: flex;
        align-items: center;
        margin-bottom: 24rpx;
        width: 48%;

        &:last-child {
          margin-bottom: 0;
        }

        .discount-label {
          width: 205rpx;
          font-size: 28rpx;
          color: #606266;
        }

        .discount-input {
          width: 240rpx;
        }

        .discount-unit {
          margin-left: 16rpx;
          color: #909399;
        }
      }
    }
  }
}
/* 主体内容区域 */
.discount-content {
  flex: 1;
}
/* 产品部分 */
.product-section {
  margin: 20rpx 0;
  padding: 20rpx 30rpx;
  border-bottom: 1px solid #eee;
}

.product-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 5rpx;
}

.product-spec {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 20rpx;
}

/* 折扣行 */
.discount-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 30rpx;
}

.discount-label {
  width: 100rpx;
  font-size: 28rpx;
  color: #333;
}

.discount-label-right {
  margin-left: 30rpx;
}

.discount-input {
  flex: 1;
  height: 70rpx;
  border: 1px solid #ddd;
  border-radius: 4rpx;
  padding: 0 15rpx;
  font-size: 26rpx;
}
.discount-row-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
}
</style>
